<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include '../../lib/utils/date.php';
include '../../lib/utils/tag.php';
include '../../model/perbuatan.php';
include '../../model/detil_perbuatan.php';
include'../../model/prestasipelanggaran.php';
include'../../model/detil_prestasipelanggaran.php';
include'../../model/karyawan.php';
include'../../model/pelapor.php';
include'../../model/dokumen.php';
include'../../model/rangking.php';
include'../../model/nilai.php';

$proc = $_REQUEST['proc'];
	
switch ($proc){
	case 'keterangan-modal':
		$deperbNo = $_REQUEST['deperbNO'];
		$DEPERB = mysql_fetch_assoc(deperb_load($deperbNo));
		?>
		<h2 class="dialog_title"><span>Keterangan</span></h2>
		<div class="dialog_content">
		<div style="border-bottom: 1px solid #CCC; padding: 20px"><?php echo $DEPERB['DEPERB_DESC']?></div>
		</div>
		<div class="dialog_buttons"><input type="button" value="Close" name="close" onclick="FBModal_hide()" /></div>
		<?php 
		break;
	case 'img-file':
		$file = $_REQUEST['file'];
		$deperbNO = $_REQUEST['deperb_no'];
		$rs = dok_load(false, $deperbNO, $file, 1);
		$row = mysql_fetch_assoc($rs);
		$imagebytes = $row['DOK_CONTENT'];
		header("Content-type: image/jpeg");
		print $imagebytes;
		break;
	case 'dokumen-modal':
		$deperbNO = $_POST['deperb_no'];
		include '../../view/client/perbuatan/perbuatan_dokumen.php';
		break;
		
	case 'perbuatan-table':
		$key = $_POST['key'];
		$status = $_POST['status'];
		$orderby = $_POST['orderby'];
		$username = $_POST['username'];
		$laporData = mysql_fetch_assoc(pelapor_load(false, $username));
		$laporID = $laporData['LAPOR_ID'];
		include '../../view/client/perbuatan/tableList.php';
		break;
		
	case 'karyawanPelaku-search-modal':
		include '../../view/admin/perbuatan/perbuatan_karyawanPelaku_search.php';
		break;
	case 'karyawanPelaku-search-list':
		$key = $_POST['key'];
		$status = $_POST['status'];
		include '../../view/admin/perbuatan/perbuatan_karyawanPelaku_search_list.php';
		break;
	case "combo-bidang":
		$achiev_jenis = $_POST['achiev_jenis'];
		$grp = perpel_select2(false, "ACHIEV_JENIS='$achiev_jenis'", "ACHIEV_BIDANG ASC", "ACHIEV_BIDANG");
        while ($grp_r = mysql_fetch_assoc($grp)){
        	echo "<option value=\"".$grp_r['ACHIEV_BIDANG']."\">".$grp_r['ACHIEV_BIDANG']."</option>";
        }
		break;
	
	case "combo-tingkat":
		$achiev_bidang = $_POST['achiev_bidang'];
		$achiev_jenis = $_POST['achiev_jenis'];
		$deprepel = deprepel_select_corelate(false, "a.ACHIEV_JENIS='$achiev_jenis' AND a.ACHIEV_BIDANG='$achiev_bidang'", "DETACHIEV_TINGKAT ASC", "DETACHIEV_TINGKAT");
		while($row = mysql_fetch_assoc($deprepel)){
			echo "<option value=\"". $row['DETACHIEV_TINGKAT']."\">". $row['DETACHIEV_TINGKAT']."</option>";
		}
		break;
		
	case "combo-kategori":
		$achiev_bidang = $_POST['achiev_bidang'];
		$achiev_jenis = $_POST['achiev_jenis'];
		$detachiev_tingkat = $_POST['detachiev_tingkat'];
		$deprepel = deprepel_select_corelate(false, "a.ACHIEV_JENIS='$achiev_jenis' AND a.ACHIEV_BIDANG='$achiev_bidang' AND b.DETACHIEV_TINGKAT='$detachiev_tingkat'", "DETACHIEV_NAMA ASC", "DETACHIEV_NAMA");
		while($row = mysql_fetch_assoc($deprepel)){
			echo "<option value=\"". $row['DETACHIEV_ID']."\">". $row['DETACHIEV_NAMA']."</option>";
		}
		break;
		
	case 'upload':
		// Validation		
		$error = false;
		if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
			$error = 'Invalid Upload';
		}		
		$size = @getimagesize($_FILES['Filedata']['tmp_name']);
		if (!$error && !$size )	{
			$error = 'Please upload only images, no other files are supported.';
		}
		else if (!$error && !in_array($size[2], array(1, 2, 3, 7, 8) ) )	{
			$error = 'Please upload only images of type JPEG, GIF or PNG.';
		}
		else if (!$error && ($size[0] < 25) || ($size[1] < 25))	{
			$error = 'Please upload an image bigger than 25px.';
		}
	
		// Processing
		if ($error) {			
			$return = array(
				'status' => '0',
				'error' => $error
			);
		} else {
			$return = array(
				'status' => '1',
				'name' => $_FILES['Filedata']['name']
			);
			// Our processing, we get a hash value from the file
			$return['hash'] = md5_file($_FILES['Filedata']['tmp_name']);
			// ... and if available, we get image data
			$info = @getimagesize($_FILES['Filedata']['tmp_name']);
			if ($info) {
				$return['width'] = $info[0];
				$return['height'] = $info[1];
				$return['mime'] = $info['mime'];
			}
			//move to tmp folder
			move_uploaded_file($_FILES['Filedata']['tmp_name'], '../../uploads/'.$_FILES['Filedata']['name']);
			$return['src'] = $_FILES['Filedata']['name'];
		}
		
		//output
		echo json_encode($return);
		break;
		
	case 'perbuatan-add':
		$inp = $_POST['inp'];
		$lapor_id = $inp[0];
		
		if (validate()){	
			//pelapor karyawan
			if (!$lapor_id){
				$karyData = mysql_fetch_assoc(kary_load($inp[1][1][1]));
				pelapor_add('', $inp[1][1][1], $karyData['NAMA_KARYAWAN']);
				$pelapor = mysql_fetch_assoc(pelapor_load(false, $inp[1][1][1]));
				$lapor_id = $pelapor['LAPOR_ID'];
				
				//alamat
				$alamat = alamat_load(false, $inp[1][1][1]);
				while ($row = mysql_fetch_assoc($alamat)){
					pelapor_addAlamat('', $lapor_id, $row['NAMA_ALAMAT'], $row['KODE_POS'], $row['KODE_AREA'], 
										$row['KOTA'], $row['PROPINSI']);
				}
				
				//tlp
				$tlp = tlp_load($inp[1][1][1]);
				while ($row = mysql_fetch_assoc($tlp)){
					pelapor_addTlp($row['NO_TELPON'], $lapor_id);
				}
			}
			
			/*	1. perbuatan save 	*/
			/*	simpan perbuatan dengan nilai 0. Pengisian POINT saat update status 	
			 */
			$ex = perb_add($inp[4], $lapor_id, date("Y-m-d", $inp[2]), date("H:i:s", $inp[2]), $inp[1][1][1], 0);
			$perb_no = mysql_fetch_assoc(perb_load_last());
			$perb_no = $perb_no['PERB_NO'];
			if (!$ex) {
				echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));	 return;
			}
			
			/*	2. detil perbuatan save 
			 *	simpan detil perbuatan :  
			 *	- count sementara diisi NOL, pada proses aktivasi nilai ini akan diupdate @see(perbuatan-add) 
			 */
			$ex = deperb_add($perb_no, $inp[9], $inp[5], date("Y-m-d", $inp[3]), date("H:i:s", $inp[3]), 0, 0, $inp[11]);
			$deperb_no = mysql_fetch_assoc(deperb_load_last());
			$deperb_no = $deperb_no['DEPERB_NO'];
			if (!$ex) {
				echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));	 return;
			}
			
			/*	3. save file	*/
			if (is_array($inp[10])){
				foreach ($inp[10] as $file){
					$size	 = filesize('../../uploads/'.$file);
					$fp      = fopen('../../uploads/'.$file, 'r');
					$content = fread($fp, $size);
					$content = addslashes($content);
					fclose($fp);
					$imgsize = @getimagesize('../../uploads/'.$file);
					if (!dok_add('', $deperb_no, addslashes($file), '', $size, $content, $imgsize[0], $imgsize[1])) {
						echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));	 return;
					}
					@unlink('../../uploads/'.$file);
				}
			}
			echo json_encode(array('error'=> false, 'msg'=> print_r($inp,true)));
		}
		break;
	case 'perbuatan-delete':
		$perbNO = $_POST['perb_no'];
		$ex = true;
		$deperbD = deperb_select(false, "PERB_NO='$perbNO'");
		while ($row = mysql_fetch_assoc($deperbD)){
			$ex = dok_delete(false, $row['DEPERB_NO']) && deperb_delete($row['DEPERB_NO']);
			if (!$ex) break; 
		}
		
		$dataPerb = mysql_fetch_assoc(perb_load($perbNO));
		if ($ex) $ex = perb_delete($perbNO);
		
		//update ranking
		$karyawanPelaku = $dataPerb['KODE_KARYAWAN'];
		$periode = strtotime($dataPerb['PERB_TGL']);
		update_rank($karyawanPelaku, $periode);
		
		echo json_encode( array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())) );
		break;
}

function validate(){
	$inp = $_POST['inp'];
	
	//waktu lapor
	if (!$inp[2]){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi waktu laporan")); return false;
	}
	
	//waktu kejadiam
	if (!$inp[3]){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi waktu kejadian")); return false;
	}
	
	//karyawan pelaku
	if (!$inp[4]){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon memilih Karyawan Pelaku")); return false;
	}
	
	//tempat kejadian
	if (!$inp[5]){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi tempat kejadian")); return false;
	}
	
	//detail prestasi pelanggaran
	if (!$inp[9]){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon memilih kategori kejadian")); return false;
	}
	
	//file
	if (!$inp[10]){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon menyertakan dokumen bukti")); return false;
	}
	
	return true;
}

function update_rank($karyawanPelaku, $periode){
	//update rangking
	$rank_point = rank_sum( $karyawanPelaku, date("m", $periode), date("Y", $periode) );
	if ( $rank_id = rank_exist($karyawanPelaku, date("m", $periode), date("Y", $periode)) ){
		$ex = rank_update($rank_id, false, false, false, $rank_point);
	}
	else{
		$ex = rank_insert('', $karyawanPelaku, 0, date("Y-m-d", $periode), $rank_point);
	}
	rank_ranked( date("m", $periode), date("Y", $periode) );	//update nomor rangking
}